package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhentao.mapper.BusinessShopMapper;
import com.zhentao.mapper.BusinessShopTypeMapper;
import com.zhentao.model.common.business.dto.BusinessShopTypeDto;
import com.zhentao.model.common.business.pojo.BusinessShop;
import com.zhentao.model.common.business.pojo.BusinessShopType;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.service.BusinessShopTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author liu
* @description 针对表【business_shop_type】的数据库操作Service实现
* @createDate 2024-09-15 11:34:22
*/
@Service
public class BusinessShopTypeServiceImpl extends ServiceImpl<BusinessShopTypeMapper, BusinessShopType>
    implements BusinessShopTypeService {
    @Resource
    private BusinessShopTypeMapper businessShopTypeMapper;

    /**
     * 店铺分类列表
     * @param businessShopTypeDto
     * @return
     */
    @Override
    public ResponseResult selShopTypeList(BusinessShopTypeDto businessShopTypeDto) {
        //分页
        PageHelper.startPage(businessShopTypeDto.getPageNum(),businessShopTypeDto.getPageSize());

        //查询列表
        List<BusinessShopType> businessShopTypeList = businessShopTypeMapper.selectList(Wrappers.<BusinessShopType>lambdaQuery()
                //根据名称模糊查
                .like(StringUtils.isNotBlank(businessShopTypeDto.getStName()), BusinessShopType::getStName, businessShopTypeDto.getStName())
        );

        PageInfo<BusinessShopType> businessShopTypePageInfo = new PageInfo<>(businessShopTypeList);
        return ResponseResult.okResult(businessShopTypePageInfo);
    }


    /**
     * 店铺分类添加
     * @param businessShopType
     * @return
     */
    @Override
    public ResponseResult addShopType(BusinessShopType businessShopType) {
        //判断是否为空
        if (businessShopType.getStName()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //进行判断是否重复
        BusinessShopType businessShopType1 = businessShopTypeMapper.selectOne(Wrappers.<BusinessShopType>lambdaQuery().eq(BusinessShopType::getStName, businessShopType.getStName()));

        if (businessShopType1!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }

        businessShopType.setCreateTime(new Date());
        businessShopTypeMapper.insert(businessShopType);

        return ResponseResult.okResult("添加成功");
    }

    /**
     * 店铺分类修改
     * @param businessShopType
     * @return
     */
    @Override
    public ResponseResult updShopType(BusinessShopType businessShopType) {
        if(businessShopType.getStName() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        BusinessShopType businessShopType2 = businessShopTypeMapper.selectOne(Wrappers.<BusinessShopType>lambdaQuery().eq(BusinessShopType::getStName, businessShopType.getStName()));
        if(businessShopType2 != null){
            return ResponseResult.errorResult(501,"数据存在");
        }

        //根据ID查询数据
        BusinessShopType businessShopType1 = businessShopTypeMapper.selectById(businessShopType.getStId());

        businessShopType1.setStName(businessShopType.getStName());
        businessShopType1.setUpdateTime(new Date());

        businessShopTypeMapper.updateById(businessShopType1);

        return ResponseResult.okResult("修改成功");
    }


    /**
     * 店铺分类删除
     * @param id 店铺分类的ID
     * @return
     */
    @Override
    public ResponseResult delShopType(Integer id) {
        businessShopTypeMapper.deleteById(id);

        return ResponseResult.okResult("删除成功");
    }

    @Override
    public ResponseResult shopTypeList() {

        return ResponseResult.okResult(businessShopTypeMapper.selectList(null));
    }

    @Override
    public ResponseResult listType() {
        List<BusinessShopType> businessShopTypes = businessShopTypeMapper.selectList(null);
        return ResponseResult.okResult(businessShopTypes);
    }

}






